package UDPSIDE;

import user.UserInfo;

import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @创建人 MBY381
 * @创建时间 2022/6/7 14:17
 * @描述 暂无
 */
public class UDPServerThread implements Runnable {
    private final DatagramSocket socket;
    private final Integer bothPort;
    //    private final InetAddress localAddress;
    private static final List<UserInfo> database = new ArrayList<>();

    public UDPServerThread(Integer port) throws SocketException, UnknownHostException {
        this.bothPort = port;
//        this.localAddress = InetAddress.getByName(address);
        this.socket = new DatagramSocket(port);
    }


    @Override
    public void run() {
        System.out.println("UDPSenderThread " + Thread.currentThread().getName() + " 启动");
        try {
            this.receive();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void receive() throws IOException {

        while (true) {

            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);//建立机房
            try {
                socket.receive(packet);//开始接受数据
            } catch (IOException e) {
                e.printStackTrace();
            }

            InetAddress address = packet.getAddress();
            int targetPort = packet.getPort();
            System.out.println("数据来自于:" + address.getHostAddress() + "  :" + targetPort);

            byte[] data = packet.getData();
            String content = new String(data, 0, packet.getLength());
            System.out.println("数据内容:" + content + " 数据长度:" + packet.getLength());
            String[] base = content.split("\\.");
            System.out.println("此用户信息: " + Arrays.toString(base));

            UserInfo userInfo = new UserInfo(base[0], base[1]);
            UserInfo testDto = userInfo;

            boolean tag = false;
            for (UserInfo userinfo : database) {
                if (testDto.equals(userinfo)) {
                    tag = true;
                }
            }

            byte[] returnArr = Boolean.toString(tag).getBytes();
            DatagramPacket returnPacket = new DatagramPacket(returnArr, returnArr.length, address, targetPort);
            socket.send(returnPacket);

            System.out.println("\n");
        }
    }

    public void close() {
        socket.close();
        System.exit(0);
    }

    public static void main(String[] args) throws IOException {
        database.add(new UserInfo("mby381", "381228"));
        database.add(new UserInfo("zzq227", "227121"));
        database.add(new UserInfo("user", "123456"));
        System.out.println("数据库中目前拥有的账户:\n");
        for (UserInfo user : database) {
            System.out.println("ID: " + user.getUserId() + "    " + "PASSWORD: " + user.getUserPwd());
        }
        System.out.println("\n");

        UDPServerThread udpReceiveThread = new UDPServerThread(9999);
        udpReceiveThread.receive();

    }
}
